<?php
/**
 * Created by PhpStorm.
 * User: BesonTD
 * Date: 15-1-31
 * Time: 下午1:42
 * 订阅相关程序
 */


if (!defined('APP_IN')) exit('Access Denied');

if (!defined('SUBSCRIBE_SIGN')) define('SUBSCRIBE_SIGN', 'mycars');

if (!isset($_GET['sign']) || $_GET['sign'] != SUBSCRIBE_SIGN) return false;

$sql = "SELECT * FROM {$db_config['TB_PREFIX']}car_trigger LIMIT 1";
$limit = 1;
while ($row = $db->row_query_one($sql)) {
    subscribe_page($row['brand_id'], $row['cid'], $row['price']);
    $db->row_delete('car_trigger', "id={$row['id']}");
    $sql = "SELECT * FROM {$db_config['TB_PREFIX']}car_trigger LIMIT " . $limit . ",1";
    $limit++;

}

return false;

/**
 * subscribe_page
 * 分页处理操作
 * $limit (每页限制条数)
 * $brand_id （品牌）
 * $cid (城市)
 * $price 价格
 */
function subscribe_page($brand_id, $cid, $price, $limit = 10)
{
    global $db, $db_config;
    $count = $db->row_count('member_rules', "(brand_id=$brand_id or brand_id=0) and (cid=$cid or cid=0) and ($price>=lower_price and $price<=higher_price) GROUP BY mid");
    $count = ceil($count / $limit);
    $sql_str = '(';
    for ($i = 1; $i <= $count; $i++) {
        $sql = "SELECT id,mid FROM {$db_config['TB_PREFIX']}member_rules WHERE (brand_id=$brand_id or brand_id=0) and (cid=$cid or cid=0) and ($price>=lower_price and $price<=higher_price) GROUP BY mid LIMIT " . ($i - 1) * $limit . ",".$limit;
        $list = $db->row_query($sql);
        if ($list) {
            foreach ($list as $v) {
                $sql_str .= $v['id'] . ',';
            }
        }
    }
    $sql_str = substr($sql_str, 0, -1);
    $sql_str .= ')';
    if ($sql_str != ')') {
        $db->query("UPDATE {$db_config['TB_PREFIX']}rule_count SET number=number+1 WHERE r_id in $sql_str");
    }
}
